package org.example.sysdept.mapper;

import org.example.Exception.SysDeptException;
import org.example.sysdept.entity.SysDept;
import org.example.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门持久层接口
 */
public class SysDeptMapper {
    /**
     * 新增部门
     * @param sysDept
     */
    public int insertSysDept(SysDept sysDept) {
        Connection conn=null;
        PreparedStatement psd=null;
        int i=0;
        String sql="insert into sys_dept(parent_id,dept_name) values (?,?)";
        try{
            conn= DBUtil.getConnection();
            psd = conn.prepareStatement(sql);
            psd.setLong(1,sysDept.getParentId());
            psd.setString(2,sysDept.getDeptName());
             i=psd.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(psd,conn,null);
        }
        return i;

    }
    /**
     * 批量新增部门
     * @param sysDeptList
     */
    public void insertBatchSysDept(List<SysDept> sysDeptList) {

    }

    public void updateBatchSysDept(List<SysDept> sysDeptList) {

    }

    public void deleteBatchSysDept(List<Long> deptIdList) {

    }

    /**
     * 查询部门信息
     * @param sysDept
     * @return
     */
    public List<SysDept> selectSysDeptList(SysDept sysDept) {
        Connection conn = null;
        PreparedStatement psd = null;
        ResultSet rs = null;
        List<SysDept> sysDeptList = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT * FROM sys_dept");

        boolean hasConditions = false;

        if (sysDept != null) {
            if (sysDept.getParentId() != null) {
                sql.append(hasConditions ? " AND " : " WHERE ").append("parent_id = ?");
                hasConditions = true;
            }

            if (sysDept.getDeptName() != null && !sysDept.getDeptName().trim().isEmpty()) {
                sql.append(hasConditions ? " AND " : " WHERE ").append("dept_name = ?");
            }
        }

        try {
            conn = DBUtil.getConnection();
            psd = conn.prepareStatement(sql.toString());

            int paramIndex = 1;

            if (sysDept != null) {
                if (sysDept.getParentId() != null) {
                    psd.setLong(paramIndex++, sysDept.getParentId());
                }

                if (sysDept.getDeptName() != null && !sysDept.getDeptName().trim().isEmpty()) {
                    psd.setString(paramIndex++,sysDept.getDeptName() );
                }
            }
            rs = psd.executeQuery();
            while (rs.next()) {
                SysDept dept = new SysDept();
                dept.setDeptId(rs.getLong("dept_id"));
                dept.setParentId(rs.getLong("parent_id"));
                dept.setAncestors(rs.getString("ancestors"));
                dept.setDeptName(rs.getString("dept_name"));
                dept.setOrderNum(rs.getInt("order_num"));
                dept.setLeader(rs.getString("leader"));
                dept.setPhone(rs.getString("phone"));
                dept.setEmail(rs.getString("email"));
                dept.setStatus(rs.getString("status"));
                dept.setDelFlag(rs.getString("del_flag"));
                dept.setCreateBy(rs.getString("create_by"));
                dept.setCreateTime(rs.getTimestamp("create_time"));
                dept.setUpdateBy(rs.getString("update_by"));
                dept.setUpdateTime(rs.getTimestamp("update_time"));
                sysDeptList.add(dept);
            }

        } catch (SQLException e) {
            throw new RuntimeException("查询部门列表失败", e);
        } finally {
            DBUtil.close(psd, conn, rs);
        }

        return sysDeptList;
    }
    /**
     * 删除部门
     * @param deptId
     * @return
     */
    public int deleteSysDept(Long deptId){
        int i=0;
        Connection conn = null;
        PreparedStatement psd = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM sys_dept WHERE dept_id = ?";
            psd = conn.prepareStatement(sql);
             psd.setLong(1, deptId);
             i = psd.executeUpdate();
        } catch (SQLException e) {}
        return i;
    }
    public int updateSysDept(SysDept sysDept) {
        int i=0;
        Connection conn = null;
        PreparedStatement psd = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "update sys_dept set dept_name=?,phone=?,email=? where dept_id=?";
            psd = conn.prepareStatement(sql);
            psd.setString(1, sysDept.getDeptName());
            psd.setString(2, sysDept.getPhone());
            psd.setString(3, sysDept.getEmail());
            psd.setLong(4, sysDept.getDeptId());
            return psd.executeUpdate();
        } catch (SQLException e) {
            throw new SysDeptException("更新部门失败");
        }
    }
    public SysDept selectSysDept(Long deptId) {
        Connection conn = null;
        PreparedStatement psd = null;
        ResultSet rs = null;
        String sql="SELECT dept_id, dept_name, phone, email FROM sys_dept WHERE dept_id = ?";
        try {
            conn = DBUtil.getConnection();
            psd = conn.prepareStatement(sql);
            psd.setLong(1, deptId);
            rs=psd.executeQuery();
            if (rs.next()) {
                SysDept sysDept = new SysDept();
                sysDept.setDeptId(rs.getLong("dept_id"));
                sysDept.setDeptName(rs.getString("dept_name"));
                sysDept.setPhone(rs.getString("phone"));
                sysDept.setEmail(rs.getString("email"));
                return sysDept;
            }
        } catch (SQLException e) {
            throw new SysDeptException("查询部门失败");
        }
        return null;
    }
}
